package com.xindaoapp.happypet.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.xindao.photonics.fragments.manager.BaseFragment;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.bean.BaseEntity;
import com.xindaoapp.happypet.bean.User;
import com.xindaoapp.happypet.ui.MainActivity;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.IRequest;
import com.xindaoapp.happypet.utils.ProgressHUD;


/**
 * 输入邀请码界面
 * 
 * @author Yangqing
 * 
 */
public class RequestCodeFragment extends BaseFragment {
    private String userName = "";
    private TextView et_input_number;
    private User user;
    private TextView input_hint, yaoqing;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.layout_request_code, container, false);
    }

    @Override
    protected void initViews(View view) {
        super.initViews(view);
        // input_hint = (TextView) getView().findViewById(R.id.input_hint);
        yaoqing = (TextView) getView().findViewById(R.id.yaoqing);
        tv_success = (TextView) getView().findViewById(R.id.tv_success);


        Bundle arguments = getArguments();
        if (arguments != null) {
            userName = arguments.getString("userName");
            user = (User) arguments.getSerializable("user");
        }
        if (user != null) {
            // input_hint.setText(user.jifentishi);
            yaoqing.setText(user.invitetip);

            tv_success.setText(Html.fromHtml(String.format(
                    getResources().getString(R.string.retister_success), user.niuniu)));

        }
    }

    @Override
    protected void initEvents() {
        super.initEvents();
        et_input_number = (TextView) getView().findViewById(R.id.et_input_number);
        getView().findViewById(R.id.btn_submit).setOnClickListener(mOnClickListener);
        getActivity().findViewById(R.id.right).setOnClickListener(mOnClickListener);
    }

    private final OnClickListener mOnClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_submit:// 提交邀请码
                    if (checkInput()) {
                        submitRequestCode();
                    }
                    break;
                case R.id.right:// 跳过
                    if (null != getActivity()) {
                        getActivity().finish();
                    } else if (null != mActivity) {
                        startActivity(new Intent(mActivity, MainActivity.class));
                    }
                    break;
            }
        }
    };
    private TextView tv_success;



    /**
     * 提交邀请码
     */
    private void submitRequestCode() {
        final ProgressHUD progressHUD =
                ProgressHUD.show(getActivity(), "正在登录请稍候...", true, true, null);
        getMoccaApi().getRequestCodeInfo(et_input_number.getText().toString().trim(),
                new IRequest<BaseEntity>() {
                    @Override
                    public void request(BaseEntity t) {
                        if (progressHUD != null) {
                            progressHUD.dismiss();
                        }
                        if (t != null && !TextUtils.isEmpty(t.getMsg())) {
                            Toast.makeText(getActivity(), t.getMessage(), Toast.LENGTH_SHORT)
                                    .show();
                            getActivity().sendBroadcast(
                                    new Intent(Constants.ACTION_WEIBA_COUNT_CHANGED));
                            getActivity().finish();
                        }
                    }
                });
    }

    /**
     * 检查输入是否为空
     * 
     * @return
     */
    private boolean checkInput() {
        if (TextUtils.isEmpty(et_input_number.getText().toString().trim())) {
            Toast.makeText(getActivity(), R.string.input_requestcode, Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    @Override
    protected String getTitleName() {
        return "填写邀请码";
    };

    @Override
    protected String getRightName() {
        return "跳过";
    }


}
